<?php

/**
 * Controller_exam
 * 考试设置
 */

class Controller_exam extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->semester = YOU::loadModel('semester');
		$this->model->grade    = YOU::loadModel('grade');
	}
	
	/** 学期列出考试 **/
	public function actionListBySemesterJson()
	{
		$semester_id = (int)$this->get->semester_id;
		$exam_lists = $this->db->fetchAll("SELECT exam_id,exam_name FROM `tbl_exam` WHERE semester_id='{$semester_id}'");
		
		foreach ($exam_lists as $key => $value){
			$data[$value['exam_id']] = $value['exam_name'];
		}
		
		echo json_encode($data);
	}
	
	/** 首页 **/
	public function actionIndex()
	{

	}

	/** 考试名称列表 **/
	public function actionList()
	{
		$semester_id = (int)$this->get->semester_id;
		$grade_id    = (int)$this->get->grade_id;
		$exam_type   = (int)$this->get->exam_type;
        // 年级列表
        $grade_lists = $this->model->grade->getAll();
		if (empty($semester_id)){
			// 最新学期id
			$semester_data = $this->model->semester->getNew();
			$semester_id   = $semester_data['semester_id'];
            //自动给本学期没有期末考试的年级加上
            foreach($grade_lists as $key => $value){
                $row = $this->db->fetchRow("SELECT * FROM `tbl_exam` WHERE grade_id='{$value['grade_id']}' AND semester_id='{$semester_id}' AND exam_type=4 AND exam_name LIKE '%期末%'");
                if(empty($row)){
                    $info = array(
                        "semester_id"  			=> $semester_id,
                        "exam_ename"  			=> "期末考",
                        "exam_name"  			=> $value['grade_name']."期末考试",//.
                        "exam_type"       		=> 4,
                        "score_type"       		=> 1,
                        "grade_id"		        => $value['grade_id'],
                        "status"                => 1,
                        "create_time"           => date("Y-m-d H:i:s"),
                        "update_time"           => "0000-00-00 00:00:00",
                    );

                    $this->db->do_insert("tbl_exam", $info);
                }
                $row = $this->db->fetchRow("SELECT * FROM `tbl_exam` WHERE grade_id='{$value['grade_id']}' AND semester_id='{$semester_id}' AND exam_type=4 AND exam_name LIKE '%期中%'");
                if(empty($row)){

                    $info = array(
                        "semester_id"  			=> $semester_id,
                        "exam_ename"  			=> "期中考",
                        "exam_name"  			=> $value['grade_name']."期中考试",
                        "exam_type"       		=> 4,
                        "score_type"       		=> 1,
                        "grade_id"		        => $value['grade_id'],
                        "status"                => 1,
                        "create_time"           => date("Y-m-d H:i:s"),
                        "update_time"           => "0000-00-00 00:00:00",
                    );

                    $this->db->do_insert("tbl_exam", $info);
                }
            }

        }

		$where = " WHERE 1=1 ";

		if (!empty($semester_id)){
			$where .= " AND semester_id='{$semester_id}' ";
			$this->tpl->assign("semester_id", $semester_id);
		}
		if (!empty($exam_type)){
			$where .= " AND exam_type='{$exam_type}' ";
			$this->tpl->assign("exam_type", $exam_type);
		}
		if (!empty($grade_id)){
			$where .= " AND grade_id='{$grade_id}' ";
			$this->tpl->assign("grade_id", $grade_id);
		}

		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_exam` {$where}");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}
		$exam_lists = $this->db->fetchAll("SELECT * FROM `tbl_exam` {$where} ORDER BY grade_id,exam_type DESC LIMIT {$start}, ".PAGE_LIMIT."");
		foreach ($exam_lists as $key => $value){
			$exam_lists[$key]['exam_type']  = $this->exam_type_ini[$value['exam_type']];
			$exam_lists[$key]['score_type'] = $this->score_type_ini[$value['score_type']];
			$exam_lists[$key]['grade_name'] = $this->model->grade->getNameById($value['grade_id']);
			$exam_lists[$key]['semester_name'] = $this->model->semester->getNameById($value['semester_id']);

		}


		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));


		// 学期列表
		$semester_lists = $this->model->semester->getAll();


		$this->tpl->assign("grade_lists", $grade_lists);
		$this->tpl->assign("semester_lists", $semester_lists);
		$this->tpl->assign("exam_lists", $exam_lists);
		$this->tpl->display('exam/exam.list.tpl.php');
	}


	/** 考试名称设置 **/
	public function actionAdd()
	{
		
		if ($_POST){

			$semester_id = $this->post->semester_id;
			$exam_name   = $this->post->exam_name;
			$exam_ename  = $this->post->exam_ename;
			$exam_type   = $this->post->exam_type;
			$score_type  = $this->post->score_type;
			$grade_ids   = $this->post->grade_ids;


			if (!empty($grade_ids)){

				foreach ($grade_ids as $grade_id){
					// 写入数据库
					$info = array(
					"semester_id"  			=> $semester_id,
					"exam_ename"  			=> $exam_ename,
					"exam_name"  			=> $exam_name,
					"exam_type"       		=> $exam_type,
					"score_type"       		=> $score_type,
					"grade_id"		        => $grade_id,
					"status"                => 1,
					"create_time"           => date("Y-m-d H:i:s"),
					"update_time"           => "0000-00-00 00:00:00",
					);

					$this->db->do_insert("tbl_exam", $info);
				}
			}
			Helper_Client::location("添加成功", "index.php?a=exam.list");

		}

		// 学期列表
		$semester_lists = $this->model->semester->getAll();
		// 年级列表
		$grade_lists = $this->model->grade->getAll();

		$this->tpl->assign("semester_lists", $semester_lists);
		$this->tpl->assign("grade_lists", $grade_lists);
		$this->tpl->display("exam/exam.add.tpl.php");
	}



	/** 修改 **/
	public function actionUpdate()
	{

		$exam_id   = (int)$this->get->exam_id;
		$exam_data = $this->db->fetchRow("SELECT * FROM `tbl_exam` WHERE `exam_id`='{$exam_id}'");

		if (empty($exam_data)){
			Helper_Client::alert("考试不存在");
		}

		if ($_POST){

			$semester_id = $this->post->semester_id;
			$exam_name   = $this->post->exam_name;
			$exam_ename  = $this->post->exam_ename;
			$exam_type   = $this->post->exam_type;
			$score_type  = $this->post->score_type;

			// 写入数据库
			$info = array(
			"semester_id"  			=> $semester_id,
			"exam_ename"  			=> $exam_ename,
			"exam_name"  			=> $exam_name,
			"exam_type"       		=> $exam_type,
			"score_type"       		=> $score_type,
			"update_time"           => date("Y-m-d H:i:s"),
			);

			$this->db->do_update("tbl_exam", $info, "exam_id='{$exam_id}'");

			Helper_Client::location("修改成功", "index.php?a=exam.list");

		}

		$exam_data['grade_name'] = $this->model->grade->getNameById($exam_data['grade_id']);
		
		// 学期列表
		$semester_lists = $this->model->semester->getAll();
		// 年级列表
		$grade_lists = $this->model->grade->getAll();

		$this->tpl->assign("semester_lists", $semester_lists);
		$this->tpl->assign("grade_lists", $grade_lists);
		$this->tpl->assign("exam_data", $exam_data);
		$this->tpl->display("exam/exam.update.tpl.php");
	}

	/** 删除 **/
	public function actionDel()
	{
		$exam_id = (int)$this->get->exam_id;
		$this->db->query("DELETE  FROM `tbl_exam` WHERE exam_id='{$exam_id}'");
		// 跳转
		Helper_Client::location("删除成功", "index.php?a=exam.list");
	}

}

?>